/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package net.dntuan.greeting;

import com.lowagie.text.Document;
import com.lowagie.text.pdf.PdfWriter;
import com.lowagie.text.Chunk;
import com.lowagie.text.Font;
import com.lowagie.text.Paragraph;
import java.io.FileOutputStream;
import java.awt.Color;

/**
 * Local and remote anchor
 * @author dang nhu tuan
 */
public class Anchor {

    public Anchor() throws Exception {

        Document document = new Document();
        PdfWriter writer = PdfWriter.getInstance(document,
                new FileOutputStream("D:\\anchor.pdf"));
        document.open();

        /**
         * Define a Font instance with BLUE and UNDERLINE styles.
         * Just to make the links in this example look like html hyperlink
         */
        Font font = new Font();
        font.setColor(Color.BLUE);
        font.setStyle(Font.UNDERLINE);

        document.add(new Chunk("Chapter 1"));

        /**
         * You can set a chunk of text to act as a link to direct reader
         * to particular content on the same PDF as they click on the link.
         */
        document.add(
                new Paragraph(
                new Chunk("Press here to go chapter 2", font).setLocalGoto("2")));

        document.newPage();

        document.add(new Chunk("Chapter 2").setLocalDestination("2"));

        /**
         * This code create a external link that display web content
         * in default internet browser.
         */
        document.add(
                new Paragraph(
                new Chunk("http://www.geek-tutorials.com", font).setAnchor("http://www.geek-tutorials.com")));

        /**
         * This code create a remote link to external PDF file
         * that located in the local drive.
         * It will open outline.pdf file and jump to destination "3".
         */
        document.add(
                new Paragraph(
                new Chunk("Open outline.pdf chapter 3", font).setRemoteGoto("outline.pdf", "3")));

        document.close();

    }

    public static void main(String[] args) {

        try {
            Anchor anchor = new Anchor();
        } catch (Exception e) {
            System.out.println(e);
        }

    }
}
